import { BaseRuntime } from "./BaseRuntime";

export default class SwitchButton extends BaseRuntime{
    name:string;
    label:string;
    info:string[];
    status:number;
    iconx:number;
    _icon:Laya.Sprite;
    _info:Laya.Label;
    _name:Laya.Text;
    constructor(){
        super();
        this.name = "switch";
        this.label = "switch";
        this.info = ["off","on"];
    }
    
    onAwake(){
        this.status = 0;
        super.onAwake();
        this.on(Laya.Event.CLICK,this,this.onClick);
        this.iconx = this._icon.x;
        this.refreshInfo();
    }

    onClick(){
        console.log("click !!!");
        this["_bg_st"+this.status].visible = false;
        this.status = (this.status+1)%2;
        this["_bg_st"+this.status].visible = true;
        this._icon.x = this.iconx + this._info.width*this.status;
        this._info.align = this.status?'left':'right';
        this.refreshInfo();
        this.parent.event('mm_click',this.name);
    }

    setData(name:string,label:string,on:string,off:string){
        this.name = name;
        this.label = label;
        this.info = [off,on];
        this.refreshInfo();
    }

    refreshInfo(){
        if(this._info){
            this._info.text = this.info[this.status];
        } 
        if(this._name){
            this._name.text = this.label;
        }
    }
}